<script setup name="Apply">
	import {ref, reactive} from 'vue'
	import {reimApi} from '@/api/reim.js'
	import {
		Delete,
		Edit,
		CloseBold,
		Search, Plus,
	} from '@element-plus/icons-vue'
	import {ElMessage, ElMessageBox} from "element-plus";
	import {useUserInfoStore} from "@/store/userInfo.js";
	//获取登录人员信息
	const userInfoStore = useUserInfoStore();
	const userLogin = ref({})
	userLogin.value = userInfoStore.getUserInfo()
	
	//表格数据
	let selfReimList = ref([])
	const total = ref(0)
	
	//分页信息，搜索条件
	const reimQuery = reactive({
		page: 1,
		limit: 10,
		explain: ''
	})
	
	const loadData = () => {
		reimApi.selectByUserId(reimQuery).then(result => {
			selfReimList.value = result.data.list
			total.value = result.data.total
		})
	}
	
	loadData()
	
	const onSearch = () => {
		reimQuery.page = 1
		loadData()
	}
	
	//单个删除
	const deleteById = (id) => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			.then(() => {
				reimApi.deleteById(id).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
				
			})
	}
	
	//批量删除
	const ids = ref([])
	const handleSelectionChange = (rows) => {
		ids.value = rows.map(row => row.id)
		console.log(ids.value);
	}
	const deleteAll = () => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			//点击确认删除
			.then(() => {
				ids.value = ids.value.join(',')
				reimApi.deleteAll(ids.value).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
			})
	}
	//撤销
	const deleted = (id) => {
		ElMessageBox.confirm(
			'确认撤销吗?【撤销不可恢复】',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			.then(() => {
				reimApi.deleted(id).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
				
			})
	}
	
	//添加、编辑
	const dialogVisible = ref(false)
	const reim = ref({})
	const title = ref('')
	const showAddDialog = () => {
		dialogVisible.value = true  //弹层出现
		title.value = '添加'
		reim.value = {}
	}
	
	const showUpdateDialog = (id) => {
		dialogVisible.value = true
		title.value = '编辑'
		reim.value = {}
		reimApi.selectById(id).then(result => {
			reim.value = result.data
		})
	}
	
	const addOrUpdate = () => {
		if (reim.value.id) {  //有值，编辑
			reimApi.update(reim.value).then(result => {
				if (result.code === 0) {
					ElMessage({message: result.msg, type: 'success',})
					dialogVisible.value = false  //弹层消失
				} else {
					ElMessage.error(result.msg)
				}
				//刷新表格
				loadData()
			})
		} else {   //无值，添加
			reimApi.add(reim.value).then(result => {
				if (result.code === 0) {
					ElMessage({message: result.msg, type: 'success',})
					dialogVisible.value = false
				} else {
					ElMessage.error(result.msg)
				}
				//刷新表格
				loadData()
			})
		}
	}
	
	
	
	//审核管理员数据
	import {adminApi} from "@/api/admin.js";
	
	const options = ref([])
	//获取审核人数据
	const selectAllAdmin = () => {
		if(userLogin.value.role==3){
			adminApi.selectByDId(userLogin.value.departmentId).then(result => {
				options.value = result.data
			})
		}else{
			adminApi.selectSuperAdmin().then(result => {
				options.value = result.data
			})
		}
	}
	selectAllAdmin()
	
	import {useTokenStore} from '@/store/token.js'
	const tokenStore = useTokenStore();
	const headers = ref({
		//携带token传递到后端
		Authorization: tokenStore.token
	});
	const handleAvatarSuccess = (result) => {
		reim.value.image = result.data
	}
	
	// 重置搜索
	const originData = () => ({explain: "",  beginCreateTime: "", endCreateTime: ""});
	const resetForm = () => {
		Object.assign(reimQuery, originData());
		loadData()
	};
</script>

<template>
	<div class="back">
		<el-card class="">
			<template #header>
				<div class="header">
					<el-button type="success" :icon="Plus" @click="showAddDialog">添加</el-button>
					<el-button type="danger" :icon="Delete" @click="deleteAll">批量删除</el-button>
				</div>
			</template>
			<!--		搜索框-->
			<el-form :inline="true" class="demo-form-inline">
				<el-form-item label="报销原因">
					<el-input v-model="reimQuery.explain" placeholder="请输入报销原因" clearable/>
				</el-form-item>
				<el-form-item label="申请时间">
					<el-date-picker
						v-model="reimQuery.beginCreateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="开始时间"
					/>
					-
					<el-date-picker
						v-model="reimQuery.endCreateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="结束时间"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" :icon="Search" @click="onSearch">搜索</el-button>
					<el-button @click="resetForm" round>重置</el-button>
				</el-form-item>
			</el-form>
			
			<!--		信息表格-->
			<el-table ref="multipleTableRef" :data="selfReimList" border stripe style="width: 100%;font-size:16px"
					  @selection-change="handleSelectionChange" height="450px" >
				<el-table-column type="selection" :selectable="selectable" min-width="50" align="center"/>
				<el-table-column prop="totalAmount" label="总金额" align="center" min-min-width="90"/>
				<el-table-column prop="alipayAccount" label="支付宝账号" align="center" min-width="120" show-overflow-tooltip/>
				<el-table-column prop="adminName" label="部门审核人" align="center" min-width="100"/>
				<el-table-column prop="explain" label="报销说明" align="center" min-width="100" show-overflow-tooltip/>
				<el-table-column prop="image" label="附件证明" align="center" min-width="150" show-overflow-tooltip>
					<template #default="scope">
						<img :src="scope.row.image" style="max-height: 40px;max-width: 120px"/>
					</template>
				</el-table-column>
				<el-table-column prop="status" label="审核状态" align="center" min-width="100">
					<template #default="scope">
						<div v-if="scope.row.status == 0">未通过</div>
						<div v-if="scope.row.status == 1">部门通过</div>
						<div v-if="scope.row.status == 2">领导通过</div>
						<div v-if="scope.row.status == 3">尚未审核</div>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="申请时间" align="center" min-width="180" sortable/>
				<el-table-column fixed="right" label="操作" min-width="240" align="center">
					<template #default="{row}">
						<el-button v-if="row.status == 3 || row.status == 1" type="info" :icon="CloseBold" round @click="deleted(row.id)">撤销</el-button>
						<el-button v-if="row.status == 3" type="primary" :icon="Edit" round @click="showUpdateDialog(row.id)">编辑</el-button>
						<el-button v-if="row.status == 0 || row.status == 2" type="danger" :icon="Delete" round @click="deleteById(row.id)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				v-model:current-page="reimQuery.page"
				v-model:page-size="reimQuery.limit"
				:page-sizes="[5, 10, 15, 20]"
				:size="size"
				:disabled="disabled"
				:background="background"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
				@change="loadData"
				style="margin-top: 20px; justify-content: flex-end"
			/>
		</el-card>
		
		<!--		添加编辑弹出框-->
		<el-dialog v-model="dialogVisible" :title="title" width="500" :lock-scroll="false">
			<el-form :model="reim">
				<el-form-item label="总&nbsp;&nbsp;&nbsp;金&nbsp;&nbsp;&nbsp;额：" :label-width="100" >
					<el-input v-model="reim.totalAmount" autocomplete="off"/>
				</el-form-item>
				<el-form-item label="支付宝账号：" :label-width="100">
					<el-input v-model="reim.alipayAccount" autocomplete="off" />
				</el-form-item>
				<el-form-item label="审&nbsp;核&nbsp;人&nbsp;员：" :label-width="100">
					<el-select v-model="reim.adminId" class="m-2"
							   clearable
							   placeholder="请选择部门审核人"
							   @change='selectAllAdmin'
							   ar='clearSelect' >
						<el-option
							v-for="item in options"
							:key="item.id"
							:label="item.name"
							:value="item.id"
						/>
					</el-select>
				</el-form-item>
			</el-form>
			<el-form-item label="附&nbsp;件&nbsp;证&nbsp;明：" :label-width="100">
				<el-upload
					class="avatar-uploader"
					action="/api/upload"
					:show-file-list="false"
					:on-success="handleAvatarSuccess"
					:headers="headers"
				>
					<img v-if="reim.image" :src="reim.image" class="avatar"/>
					<el-icon v-else class="avatar-uploader-icon">
						<Plus/>
					</el-icon>
				</el-upload>
			</el-form-item>
			<el-form-item label="报&nbsp;销&nbsp;原&nbsp;因：" :label-width="100">
				<el-input v-model="reim.explain" type="textarea" />
			</el-form-item>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="dialogVisible = false">取消</el-button>
					<el-button type="primary" @click="addOrUpdate">
						确认
					</el-button>
				</div>
			</template>
		</el-dialog>
	</div>

</template>

<style scoped>
	.avatar-uploader .avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
<style>
	.avatar-uploader .el-upload {
		border: 1px dashed var(--el-border-color);
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		transition: var(--el-transition-duration-fast);
	}
	
	.avatar-uploader .el-upload:hover {
		border-color: var(--el-color-primary);
	}
	
	.el-icon.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		text-align: center;
	}
</style>